package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.*;
import com.itheima.service.CommentService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @GetMapping("/list")
    public Result<List<Comment>> CommentList(@RequestParam Integer articleId){
        List<Comment> list = commentService.getCommentList(articleId);
        List<User> allUserInfo = userService.getAllUserInfo();
        List<SubComment> subList = commentService.getSubCommentList();
        for (SubComment subComment:subList){
            for (User userInfo:allUserInfo){
                if (subComment.getCreateUser() == userInfo.getId()){
                    subComment.setUsername(userInfo.getNickname());
                    subComment.setUserPic(userInfo.getUserPic());
                    break;
                }
            }
        }
        for (Comment comment:list){
            comment.setSublist(subList.stream().filter(subComment -> comment.getId().equals(subComment.getFatherId())).collect(Collectors.toList()));
        }

        for (Comment comment:list){
            for (User userInfo:allUserInfo){
                if (comment.getCreateUser() == userInfo.getId()){
                    comment.setUsername(userInfo.getNickname());
                    comment.setUserPic(userInfo.getUserPic());
                    break;
                }
            }
        }

        return Result.success(list);
    }

    @PostMapping
    public Result addComment(@RequestBody @Validated Comment comment){

        commentService.addComment(comment);

        return Result.success();
    }

    @PostMapping("/addSub")
    public Result addSub(@RequestBody @Validated SubComment subComment){
        commentService.addSub(subComment);

        return Result.success();
    }

    @GetMapping("/manageComment")
    public Result<PageBean<Comment>> manageComment(Integer pageNum, Integer pageSize, @RequestParam(required = false) String content){
        PageBean<Comment> commentPageBean = commentService.manageComment(pageNum, pageSize, content);
        return Result.success(commentPageBean);
    }

    @DeleteMapping("/deleteComment")
    public Result deleteComment(Integer id){
        commentService.deleteComment(id);
        return Result.success();
    }

    @GetMapping("/manageSubComment")
    public Result<PageBean<SubComment>> manageSubComment(Integer pageNum, Integer pageSize, @RequestParam(required = false) String content){
        PageBean<SubComment> SubcommentPageBean = commentService.manageSubComment(pageNum, pageSize, content);
        return Result.success(SubcommentPageBean);
    }

    @DeleteMapping("/deleteSubComment")
    public Result deleteSubComment(Integer id){
        commentService.deleteSubComment(id);
        return Result.success();
    }
}
